<?php

include_once("includes/Sigma.php");
include_once("includes/connect.php");

function cliente_nombre($idventa){
	$sql = "SELECT c.nombre FROM ventas v JOIN cliente c ON (v.idcliente = c.idcliente) WHERE idventa = '".$idventa."'";
	$r = mysql_query($sql);
	$cliente = mysql_fetch_array($r);
	return $cliente['nombre']; 
}

$tpl = new HTML_Template_Sigma( "templates" ,"cache");
$tpl->loadTemplatefile("movimientos_stock.html", true, true);

$sql_sucursal = "SELECT nombre, idsucursal FROM `sucursales` WHERE activa = 1";
$sucursal = mysql_query($sql_sucursal);
$sucursal = mysql_fetch_array($sucursal);
$sucursal_nombre = $sucursal['nombre'];
$sucursal_id = $sucursal['idsucursal'];


$sql_movimientos = "SELECT * FROM movimientos_stock WHERE DATE(fecha) = CURDATE() ORDER BY fecha desc";
$sql_stock = "SELECT s.stock, p.descri, p.preciolt1, ptc.id as ptc, p.codsussan, ptc.talle, c.nombre as color
			  FROM stock s 
			  JOIN producto_talle_color ptc ON (s.ptc = ptc.id) 
			  JOIN productos p ON (p.codsussan = ptc.codsussan) 
			  LEFT JOIN colores c ON (ptc.color = c.idcolor)
			  WHERE ptc in 
				(SELECT ptc FROM movimientos_stock WHERE DATE(fecha) = CURDATE() ORDER BY fecha desc)
			  AND sucursal = ".$sucursal_id; 

$fecha_consulta = date("Y-m-d");

if(isset($_GET['fecha'])){
	$sql_movimientos = "SELECT * FROM movimientos_stock WHERE DATE(fecha) = '".$_GET['fecha']."' ORDER BY fecha desc";
	$sql_stock = "SELECT s.stock, p.descri, p.preciolt1, ptc.id as ptc, p.codsussan, ptc.talle, c.nombre as color
	              FROM stock s 
				  JOIN producto_talle_color ptc ON (s.ptc = ptc.id) 
				  JOIN productos p ON (p.codsussan = ptc.codsussan) 	
				  LEFT JOIN colores c ON (ptc.color = c.idcolor)
				  WHERE ptc in 
					(SELECT ptc FROM movimientos_stock WHERE DATE(fecha) = '".$_GET['fecha']."' ORDER BY fecha desc)
				  AND sucursal = ".$sucursal_id;
	$fecha_consulta = $_GET['fecha'];
}
if (isset($_GET['all'])){
	$sql_movimientos = "SELECT * FROM movimientos_stock ORDER BY fecha desc";
	
	$sql_stock = "SELECT s.stock, p.descri, p.preciolt1, ptc.id as ptc, p.codsussan, ptc.talle, c.nombre as color
				  FROM stock s 
				  JOIN producto_talle_color ptc ON (s.ptc = ptc.id) 
				  JOIN productos p ON (p.codsussan = ptc.codsussan) 				  
				  LEFT JOIN colores c ON (ptc.color = c.idcolor)
				  WHERE ptc in 
					(SELECT ptc FROM stock ORDER BY fecha desc)
				  AND sucursal = ".$sucursal_id;
}

$tpl->setVariable('fecha_consulta', $fecha_consulta);
$tpl->setVariable('sucursal', $sucursal_nombre);


$result = mysql_query($sql_stock);
$i = 0;
while($f = mysql_fetch_array($result)){
	$i+= 1;
	$color_fila = "even";
	if($i % 2 == 1){
		$color_fila = "odd";
	}


	$tpl->setVariable('ptc', $f['ptc']);
	$tpl->setVariable('codsussan', $f['codsussan']);
	$tpl->setVariable('descri', $f['descri']);
	$tpl->setVariable('talle', $f['talle']);
	$tpl->setVariable('color', $f['color']);
	$tpl->setVariable('preciolt1', $f['preciolt1']);
	$tpl->setVariable('stock', $f['stock']);
	$tpl->setVariable('color_fila', $color_fila);
	$tpl->parse("stock");
}

$result = mysql_query($sql_movimientos);
$i = 0;
while($f = mysql_fetch_array($result)){
	$cliente_nombre = "N";
	$operacion = $f['operacion'];
	if($operacion == "venta"){
		$cliente_nombre = cliente_nombre($f['idventa']);
	}
	
	$i+= 1;
	$color = "even";
	if($i % 2 == 1){
		$color = "odd";
	}

	$tpl->setVariable('ptc', $f['ptc']);
	$tpl->setVariable('stock_previo', $f['stock_previo']);
	$tpl->setVariable('stock_nuevo', $f['stock_nuevo']);
	$tpl->setVariable('dif', $f['stock_nuevo'] - $f['stock_previo']);
	$tpl->setVariable('operacion', strtoupper($f['operacion']));
	$tpl->setVariable('observacion', $f['observacion']);
	$tpl->setVariable('fecha', $f['fecha']);
	$tpl->setVariable('cliente_nombre', $cliente_nombre);
	$tpl->setVariable('color', $color);
	
	$tpl->parse("movimientos");

}

$tpl->show();


?>